﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Data;

namespace AzureTracer.Views.Behaviors
{
    public class DataBindTextChangedBehavior: Behavior<TextBox>
    {

        private BindingExpression BindingExpression { get; set; }

        protected override void OnAttached()
        {
            base.OnAttached();
            BindingExpression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
            AssociatedObject.TextChanged += AssociatedObject_TextChanged;
        }        

        protected override void OnDetaching()
        {
            base.OnDetaching();
            BindingExpression = null;
        }

        void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (BindingExpression != null)
                BindingExpression.UpdateSource();
        }
    }
}
